import java.util.Scanner;

public class Test {
    //String的练习题：
    //1、转换成小写字母
    //https://leetcode.cn/problems/to-lower-case/submissions/556060449/
    //第一种:
    public String toLowerCase1(String s) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            //判断是否是字母
            if(Character.isLetter(ch)) {
                //如果是就进来
                //再次判断是否是大写字母
                if(Character.isUpperCase(ch)) {
                    //大写字母进进来，并且转换成小写字母
                    ch = (char) (ch + 32);
                }
            }
            stringBuffer.append(ch);
        }
        return stringBuffer.toString();//返回String类型
    }
    //第二种: 位运算
//    用位运算的技巧：
//    大写变小写、小写变大写：字符 ^= 32;
//    大写变小写、小写变小写：字符 |= 32;
//    大写变大写、小写变大写：字符 &= 33;
    public String toLowerCase2(String s) {
        if(s == null || s.length() == 0) {
            return null;
        }
        char[] ch = s.toCharArray();//将此字符串转换为新的字符数组。
        for (int i = 0; i < s.length(); i++) {
            ch[i] |= 32;
        }
        return String.valueOf(ch);
    }
    //2、字符串当中的单词数
    //https://leetcode.cn/problems/number-of-segments-in-a-string/
    public int countSegments(String s) {
        if(s.length() == 0) {
            return 0;
        }
        String[] str = s.split(" ");
        int count = 0;
        for (String str2 : str) {
            if(str2.length() != 0) {
                count++;
            }
        }
        return count;
    }
    //3、字符串中第一个唯一字符
    //https://leetcode.cn/problems/first-unique-character-in-a-string/description/
    public int firstUniqChar(String s) {
        int[] count = new int[256];
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            count[ch - 'a']++;
        }
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(count[ch - 'a'] == 1) {
                return i;
            }
        }
        return -1;
    }
    //4、字符串最后一个单词的长度
    //https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da?
    //tpId=37&&tqId=21224&rp=5&ru=/activity/oj&qru=/ta/huawei/question-ranking
    public void lastCharlength() {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        //1.直接使用split()
        String[] str2 = str.split(" ");
        int count = str2[str2.length - 1].length();
        System.out.print(count);
        //2.找到最后一个空格的下标，然后截取字符串，然后求长度
        int index = str.lastIndexOf(' ');//返回最后一个空格的下标，从后往前找，第一个空格就是最后一个空格
        String str3 = str.substring(index + 1,str.length());//最后一个字符
        int count2 = str3.length();
        System.out.println(count2);
    }
    //5、验证回文串
    //https://leetcode.cn/problems/valid-palindrome/
    public static boolean isValidChar(char ch) {
        if(Character.isDigit(ch) || Character.isLetter(ch)) {
            return true;
        }
        return false;
    }
    public boolean isPalindrome(String s) {
        s = s.toLowerCase();//
        int left = 0;
        int right = s.length() - 1;
        while(left < right) {
            while(left < right && !isValidChar(s.charAt(left))) {
                left++;
            }
            while(left < right && !isValidChar(s.charAt(right))) {
                right--;
            }
            if(s.charAt(left) == s.charAt(right)) {
                left++;
                right--;
            }else {
                return false;
            }
        }
        return true;
    }
}
